//
//  StoreMainViewController.m
//  ArtDog
//
//  Created by dllo on 16/8/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "StoreMainViewController.h"
#import "BaseCollectionView.h"
#import "HLMacro.h"
#import "BannerCollectionViewCell.h"
#import "HeadReusableView.h"
#import "ShufView.h"
#import "MJRefresh.h"
#import "MJRefreshComponent+RefreshStyle.h"
#import "StoreArtListCollectionCell.h"
#import "ZWYNetTool.h"
#import "NSString+Common.h"
#import "ImageViewCollectionCell.h"
#import "StoreGoodsCollectionCell.h"
#import "UIColor+Common.h"

// Model
#import "StoreMainModel.h"
#import "HomeExhibitWorkList.h"
#import "StoreDerivativeList.h"
#import "StoreGoodsCategoriesList.h"
#import "StoreBanners.h"


@interface StoreMainViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) BaseCollectionView *collectionView;
@property (nonatomic, assign) NSInteger pageIndex;
@property (nonatomic, strong) StoreMainModel *mainModel;

@property (nonatomic, strong) NSArray *imageArray;

@property (nonatomic, strong) NSArray *headArray;

@end

@implementation StoreMainViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    self.title = @"商城";
    [self createCollectionView];
    [self refreshCollectionView];
    self.isHaveActivity = YES;
}

- (void)handleData
{
    _imageArray = [NSArray array];
    _headArray = @[@"艺术衍生", @"艺术狗定制", @"原创艺术"];
    NSString *urlString = [NSString getURLStringWith:HOST andPaths:@[@"artgoer/api/v1/user", [NSString getUserDefaultId], @"goods/indexHome"] andParams:@{@"token": [NSString getUserDefaultToken]}];
    [ZWYNetTool GET:urlString andBody:nil andHeader:HOSTHEADER andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.isHaveActivity = NO;
        _imageArray = @[@"art_customization", @"art_derive", @"art_original", @"art_convert"];
        _mainModel = [StoreMainModel modelWithDic:result[@"data"]];
        [_collectionView.mj_header endRefreshing];
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        [_collectionView.mj_header endRefreshing];
        NSLog(@"%@", error);
        self.isHaveActivity = NO;
    }];
}

- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    _collectionView = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) collectionViewLayout:layout];
    [self.view addSubview:_collectionView];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerClass:[HeadReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headReusableView"];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    [_collectionView registerNib:[UINib nibWithNibName:@"BannerCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"bannerCollectionViewCell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"ImageViewCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"imageViewCollectionCell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"StoreArtListCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"storeArtListCollectionCell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"StoreGoodsCollectionCell" bundle:[NSBundle mainBundle]]  forCellWithReuseIdentifier:@"storeGoodsCollectionCell"];
    
}

#pragma mark - 刷新
- (void)refreshCollectionView
{
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self handleData];
    }];
    [_collectionView.mj_header mjRefreshStateHeaderStyle];
    
}

#pragma mark - collectionView 协议方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 5;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    } else if (section == 1) {
        return _mainModel.goodsCategoriesList.count;
    } else if (section == 2) {
        return _mainModel.derivativeList.count;
    } else if (section == 3) {
        return _mainModel.customizeList.count;
    } else {
        return _mainModel.artList.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        BannerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"bannerCollectionViewCell" forIndexPath:indexPath];
        NSMutableArray *images = [NSMutableArray array];
        for (StoreBanners *model in _mainModel.banner) {
            [images addObject:model.picUrl];
        }
        cell.shufView.images = images;
        cell.shufView.timeInterval = 5;
        return cell;
    } else if (indexPath.section == 1) {
        ImageViewCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imageViewCollectionCell" forIndexPath:indexPath];
        if (_imageArray != nil) {
            cell.image = _imageArray[indexPath.item];
        }
        return cell;
    } else if (indexPath.section == 2 || indexPath.section == 3) {
        StoreGoodsCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"storeGoodsCollectionCell" forIndexPath:indexPath];
        if (indexPath.section == 2) {
            cell.derivativeModel = _mainModel.derivativeList[indexPath.item];
        } else {
            cell.derivativeModel = _mainModel.customizeList[indexPath.item];
        }
        return cell;
    } else {
        StoreArtListCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"storeArtListCollectionCell" forIndexPath:indexPath];
        cell.artModel = _mainModel.artList[indexPath.item];
        return cell;
    }
    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 || indexPath.section == 1) {
        return nil;
    } else if (kind == UICollectionElementKindSectionHeader) {
        HeadReusableView *headerV = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headReusableView" forIndexPath:indexPath];
        headerV.titleLabel.text = _headArray[indexPath.section - 2];
        return headerV;
    } else {
        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
        footer.backgroundColor = [UIColor colorFromHexRGB:@"eeeeee"];
        
        return footer;
    }
}

#pragma mark - flow layout delegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH / 1.8);
    } else if (indexPath.section == 1) {
        return CGSizeMake((SCREEN_WIDTH - 5) / 2, (SCREEN_WIDTH - 5) / 4);
    } else if (indexPath.section == 2 || indexPath.section == 3) {
        return CGSizeMake((SCREEN_WIDTH - 5) / 2, SCREEN_WIDTH * 0.6 + 80);
    } else {
        return CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH * 0.85 + 100);
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 1) {
        return UIEdgeInsetsMake(5, 0, 0, 0);
    }
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0 || section == 1) {
        return CGSizeZero;
    }
    return CGSizeMake(SCREEN_WIDTH, 60);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    if (section == 0 || section == 1) {
        return CGSizeZero;
    }
    return CGSizeMake(SCREEN_WIDTH, 15);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
